import { defineStore } from 'pinia'
import { store } from '../'
import { SKU } from './cart'
import useHttp from '@renderer/utils/http'
import { SPU } from '@renderer/components/list/type'
export interface ICategory {
  // spu商品
  spu: SPU | null
  // sku列表
  skuList: SKU[] | null
  // 规格对应的属性列表
  attrs: any[]
  // 规格列表
  category: any[]
  // 选中的sku
  selectedSku: SKU | null
  // 套餐选中的sku
  selectedComboSku: SKU[] | null
  // 套餐SPU商品
  comboSpu: SPU[] | null
  // 套餐
  combo: any
  // 套餐sku列表
  comboSkuList: SKU[][] | null
}
export const useCategoryStore = defineStore({
  id: 'Category',
  state: (): ICategory => ({
    spu: null,
    skuList: null,
    attrs: [],
    category: [],
    selectedSku: null,
    comboSpu: [],
    combo: null,
    selectedComboSku: [],
    comboSkuList: null
  }),
  getters: {
    comboName(state: ICategory) {
      return state.combo?.name
    },
    name(state: ICategory) {
      return state.spu?.name
    },
    defaultSku(state: ICategory) {
      return state.skuList?.find((sku) => Number(sku.is_default) === 1)
    },
    defaultComboSku(state: ICategory) {
      // @todo
      return state.comboSkuList?.reduce((prev, skuList) => {
        const sku = skuList.find((sku) => Number(sku.is_default) === 1)
        if (sku) {
          prev.push(sku)
        }
        return prev
      }, [])
    },
    skus(state: Category) {
      return state.skuList
    },
    sku(state: Category) {
      const sku = state.selectedSku || state.defaultSku || []
      if (state.spu?.imgs && typeof state.spu?.imgs === 'string') {
        sku.url = JSON.parse(state.spu.imgs)?.[0]?.url
      } else if (state.spu?.imgs && Array.isArray(state.spu?.imgs)) {
        sku.url = state.spu?.imgs?.[0]?.url
      }
      return sku
    },
    // 选中的套餐里面的sku列表
    comboSku(state: ICategory) {
      return state.selectedComboSku.length > 0
        ? state.selectedComboSku
        : state.defaultComboSku || []
    },
    // 套餐打折价格
    getComboPrice(state: ICategory) {
      return Math.ceil(
        state.comboSku.reduce((prev, sku) => {
          prev += (Number(sku.price) * Number(sku.discount)) / 10
          return prev
        }, 0)
      )
    },
    // 套餐原始价
    getComboOriginPrice(state: ICategory) {
      return Math.ceil(
        state.comboSku.reduce((prev, sku) => {
          prev += Number(sku.price)
          return prev
        }, 0)
      )
    },
    getPrice(state: Category) {
      return state.sku?.price
    },
    getOriginPrice(state: Category) {
      return null
    }
  },
  actions: {
    select(sku: SKU | null | undefined) {
      this.sku = sku
    },
    async setSkus(spuId: number, list) {
      const { data: res } = await useHttp(`/product/skus?id=${spuId}`).get().json()
      if (res.value.code === 200) {
        const skus = res.value.data.map((s) => {
          if (!s.url) {
            s.url = list.imgs[0]?.url
          }
          return s
        })
        this.skuList = skus
        return skus
      }
      return []
    },
    setSelectedSku(sku: SKU | null) {
      this.selectedSku = sku
    },
    setSpu(spu) {
      this.spu = spu
    },
    setComboSpu(spu) {
      this.comboSpu = spu
    },
    setCombo(combo) {
      this.combo = combo
    },
    setComboSkus(skus: SKU[]) {
      this.comboSkuList = skus
    },
    setSelectedComboSku(sku: SKU | null) {
      if (!sku) {
        this.selectedComboSku?.splice(0, this.selectedComboSku?.length)
      } else {
        const index = this.selectedComboSku?.findIndex((old) => old.spu_id === sku.spu_id)
        if (index > -1) {
          this.selectedComboSku?.splice(index, 1, sku)
        } else {
          this.selectedComboSku?.push(sku)
        }
      }
    }
  }
})

export const useCategoryStoreWithout = () => useCategoryStore(store)
